<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    /**
     * 加载登录模板
     */
    public function loginForm()
    {
        return view('admin.login');
    }

    /**
     * 后台登录验证
     */
    public function login(Request $request)
    {

        // 用validate验证用户名和密码是否正确
        $this->validate(
            $request,[
                'username' => 'required',
                'password' => 'required'
            ],[
                'username.required' => '用户名不能为空',
                'password.required' => '密码不能为空'
            ]);
        $status=\Auth::guard('admin')->attempt(
            [
                'username'=>$request->username,
                'password'=>$request->password,
            ]
        );

        if($status){
            // 登录成功，跳转去首页
            return redirect()->route('admin.admin')->with('success','登录成功');
        }
        return back()->with('error','登录失败，请重新登录');
    }

    public function logout(){
        // 调用auth类里面的logout方法退出登录，须经过守卫
        \Auth::guard('admin')->logout();
        return redirect()->route('admin.login')->with('success','退出成功');
    }
}
